package com.shiikong_memory.yez.KOOKBasics;

import com.mysql.cj.protocol.x.Notice;
import com.shiikong_memory.yez.MySQLAPI.DaoImpl.NoticeServer;
import com.shiikong_memory.yez.MySQLAPI.JavaBean.NoticeBean;
import love.forte.simbot.application.Application;
import love.forte.simbot.bot.BotManager;
import love.forte.simbot.bot.BotManagers;
import love.forte.simbot.common.id.Identifies;
import love.forte.simbot.component.kook.KookChatChannel;
import love.forte.simbot.component.kook.KookGuild;
import love.forte.simbot.component.kook.bot.KookBot;
import love.forte.simbot.component.kook.bot.KookBotManager;
import love.forte.simbot.component.kook.event.KookChannelMessageEvent;
import love.forte.simbot.kook.api.message.SendChannelMessageApi;
import love.forte.simbot.quantcat.common.annotations.ContentTrim;
import love.forte.simbot.quantcat.common.annotations.Filter;
import love.forte.simbot.quantcat.common.annotations.Listener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

/**
 * 广播消息到所以已登记频道
 */
@Component
public class Broadcast {
    @Autowired
    NoticeServer noticeServer;

    @Autowired
    private Application application;

    @Listener
    @ContentTrim
    @Filter(targets = @Filter.Targets(atBot = true))
    public CompletableFuture<?> setBroadcast(KookChannelMessageEvent event){
        System.out.println(event.getSourceEvent().getContent());
        if (!event.getSourceEvent().getContent().contains("允许广播功能")) return null;
        String targetId = event.getSourceEvent().getTargetId().toString();
        if (noticeServer.getByChatChannelId(targetId) != null){
            return event.replyAsync("该频道已开启允许广播功能，无需再次开启");
        }
        KookGuild guild = event.getBot().getGuildRelation().getGuild(event.getSource().getId());
        KookChatChannel kookChatChannel = guild.getChatChannel(event.getContent().getId());
        noticeServer.addNotice(kookChatChannel.getId().toString(), kookChatChannel.getName(), guild.getId().toString(), event.getBot().getId().toString());
        return event.replyAsync("允许广播功能开启成功");
    }

    public void senndAllMessage(Integer type, String message){
         for (NoticeBean noticeBean:noticeServer.getAll()){
             BotManagers botManagers = application.getBotManagers();
             for (BotManager manager : botManagers) {
                 if (manager instanceof KookBotManager kookBotManager) {
                     KookBot bot = (KookBot) kookBotManager.get(Identifies.of(noticeBean.getBotId()));
                     KookGuild guild = bot.getGuildRelation().getGuild(Identifies.of(noticeBean.getGuildId()));
                     if (guild == null) {
                         System.err.println("无法查询到服务器id：" + noticeBean.getGuildId());
                         return;
                     }
                     KookChatChannel kookChatChannel = guild.getChatChannel(Identifies.of(noticeBean.getChatChannelId()));
                     if (kookChatChannel == null) {
                         System.err.println("无法查询到服务器：" + noticeBean.getGuildId() + "的频道：" + noticeBean.getChatChannelName());
                         return;
                     }
                     kookChatChannel.sendBlocking(SendChannelMessageApi.create(type,noticeBean.getChatChannelId(), message));
                 }
             }
         }
    }
}
